import Vue from 'vue'
import Vuex from 'vuex'
// 1. 导入包
import createPersistedState from 'vuex-persistedstate'
// 1. 导入 axios
import axios from 'axios'

Vue.use(Vuex)

export default new Vuex.Store({
  plugins: [createPersistedState()],
  state: {
    token: "",
    userInfo: {}
  },
  mutations: {
    undateToken(state, newToken) {
      state.token = newToken
    },
    undateuserInfo(state, info) {
      state.userInfo = info
    }
  },
  actions: {
    async inituserInfo(ctx) {
      const {
        data: res
      } = await axios.get("/my/userinfo")
      if (res.code === 0) {
        ctx.commit("undateuserInfo", res.data)
      }
    }
  }
})
